package wap._2015.game._2;

import java.util.Scanner;

public class Main {
	
	public static void main(String[] args) {
		// Inputs
		Scanner in = new Scanner(System.in);
		int n = in.nextInt();
		int m = in.nextInt();
		int[][] map = new int[n + 1][m + 1];
		for (int i = 1; i <= n; i++)
			for (int j = 1; j <= m; j++)
				map[i][j] = in.nextInt();
		in.close();
		
		// Calculate
		int[] nowColumn = new int[n + 1];
		int[] lastColumn = new int[n + 1];
		for (int i = 1; i <= m; i++) {
			int[] _c = nowColumn;
			nowColumn = lastColumn;
			lastColumn = _c;
			
			for (int x = 1; x <= n; x++)
				nowColumn[x] = -1;
			
			for (int x = 1; x <= n; x++) {
				if (map[x][i] < 0)
					continue;
				
				// get in from the same row
				if (lastColumn[x] >= 0)
					nowColumn[x] = lastColumn[x] + map[x][i];
				
				// get in from the row below X and no transfer from floor
				int y = x - 1;
				int sum = map[x][i];
				while (y > 0 && map[y][i] > 0) {
					sum += map[y][i];
					if (lastColumn[y] >= 0)
						nowColumn[x] = Math.max(nowColumn[x], lastColumn[y] + sum);
					y--;
				}
				
				// get in from the row above X and transfer from ceil
				if (y <= 0 && sum > nowColumn[x]) {
					for (y = n; y > x; y--)
						if (map[y][i] >= 0 && lastColumn[y] >= 0) {
							nowColumn[x] = sum;
							break;
						}
				}
				
				// get in from the row above X and no transfer from ceil
				y = x + 1;
				sum = map[x][i];
				while (y <= n && map[y][i] > 0) {
					sum += map[y][i];
					if (lastColumn[y] >= 0)
						nowColumn[x] = Math.max(nowColumn[x], lastColumn[y] + sum);
					y++;
				}
				
				// get in from the row above X and transfer from ceil
				if (y > n && sum > nowColumn[x]) {
					for (y = 1; y < x; y++)
						if (map[y][i] >= 0 && lastColumn[y] >= 0) {
							nowColumn[x] = sum;
							break;
						}
				}
			}
		}
		int ans = -1;
		for (int i = 1; i <= n; i++)
			ans = Math.max(ans, nowColumn[i]);
		
		// Outputs
		System.out.println(ans);
	}

}

//TEST 1 : 23
//4 4
//-1 4 5 1
//2 -1 2 4
//3 3 -1 3
//4 2 1 2

//TEST 2 : 16
//4 4
//-1 4 5 1
//2 -1 2 4
//3 3 -1 -1
//4 2 1 2

//TEST 3 : -1
//4 5
//-1 -1 -1 -1 -1
//5 -1 2 3 1
//2 4 -1 7 1
//2 0 9 -1 9

//TEST 4 : 22
//4 5
//-1 0 0 2 1
//5 -1 2 3 1
//2 4 -1 7 1
//2 0 9 -1 9

//TEST 5 : -1
//4 5
//-1 0 -1 2 1
//5 -1 2 3 1
//2 4 -1 7 1
//2 0 9 -1 9

//TEST 6 : 33
//4 10
//-1  0  1  0  1  9  1  7 -1  1 
// 5 -1  2  3  1  2  0 -1  1  9
// 2  4 -1  7  1  0 -1  1  9  7
// 2  0  9 -1  9 -1  1  3  0  2

//TEST 7 : 24
//4 5
//1 -1 9 -1 2
//2 -1 8 -1 2
//3 4 1 0 2
//1 0 2 -1 2
